<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-10
 * Time: 16:25
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\Rank as RankModel;
use think\Db;

class Sale extends Admin
{

    //代理列表
    public function index()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $btn_access = [
            'title' => '新增代理',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $data_list = DB::name('qt_sale')->where($map)
            ->order($order)
            ->paginate()
            ->each(function ($item){
                $storeCount = Db::name('qt_store')->where('sale_id',$item['id'])->count();
                $pidName = Db::name('qt_sale')->where('id',$item['pid'])->value('name');
                if($pidName == null){
                    $item['pid_name'] = "暂无上级";
                }else{
                    $item['pid_name'] = $pidName;
                }
                $item['store_count'] = $storeCount;
                return  $item;
            });
        // 分页数据
        $page = $data_list->render();

        $btn_access1 = [
            'title' => '编辑代理',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access2 = [
            'title' => '删除代理',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default',
            'href' => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('name', '代理人', 'text')
            ->addColumn('phone', '手机号', 'text')
            ->addColumn('pid_name', '上级代理', 'text')
            ->addColumn('store_count', '门店数量', 'text')
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('Rank')// 指定数据表名
            ->setSearchArea([
                ['text:2','name','代理人','like'],
                ['text:2','phone','手机号','eq'],
            ])
            ->addTopButton('add', $btn_access)// 添加顶部按钮
            ->addRightButton('edit', $btn_access1)// 添加右侧按钮
            ->addRightButton('delete', $btn_access2)// 添加右侧按钮
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('代理列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加代理
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Sale');
            if (true !== $result) $this->error($result);
            if (Db::name('qt_sale')->insert($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {
            $pidData = Db::name('qt_sale')->column('name','id');
            $pidData[0] = '顶级代理';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '代理人'],
                        ['text:7', 'phone', '手机号'],
                        ['select:7', 'pid', '上级代理','',$pidData,'0'],
                    ]
                )
                ->setTrigger('type','1','validity')
                ->setPageTitle('添加会员折扣')
                ->fetch();
        }
    }

    //编辑代理
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Sale');
            if (true !== $result) $this->error($result);
            if (Db::name('qt_sale')->where(['id'=>$id])->update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Db::name('qt_sale')->where(['id'=>$id])->find();
            $pidData = Db::name('qt_sale')->column('name','id');
            $pidData[0] = '顶级代理';
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'name', '代理人'],
                        ['text:7', 'phone', '手机号'],
                        ['select:7', 'pid', '上级代理','',$pidData,'0'],
                    ]
                )
                ->setTrigger('type','1','validity')
                ->setFormData($info)
                ->setPageTitle('编辑代理')
                ->fetch();
        }
    }

    //删除代理
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $find = Db::name('qt_store')->where(['sale_id'=>$id])->find();
        if(!empty($find)){
            $this->error('当前代理下面有门店，禁止删除', 'index');
        }
        $re = DB::name('qt_sale')->where(['id'=>$id])->delete();//
        if ($re) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('系统繁忙请稍后再试', 'index');
        }

    }
}